//
//  KATColors.m
//  KATFramework
//
//  Created by Kantice on 14-9-2.
//  Copyright (c) 2014年 KatApp. All rights reserved.
//

#import "KATColor.h"

@implementation KATColor


//通过HSBA获取颜色(前2~3位为色相，取值范围为0~359度，之后2位为饱和度，取值范围为0~99,最后2位为透明度，取值范围为0~99)
+ (UIColor *)colorWithHSBA:(int)HSBA
{
    CGFloat h=0,s=0,b=0,a=0;
    
    h=(HSBA/COLOR_HSBA_H%360)/360.0;
    s=(HSBA%COLOR_HSBA_H/COLOR_HSBA_S)/99.0;
    b=(HSBA%COLOR_HSBA_S/COLOR_HSBA_B)/99.0;
    a=(HSBA%COLOR_HSBA_B/COLOR_HSBA_A)/99.0;
    
    return [UIColor colorWithHue:h saturation:s brightness:b alpha:a];
}


//通过颜色获取HSBA值
+ (KATFloatArray *)HSBAValueFromColor:(UIColor *)color
{
    if(color && [color respondsToSelector:@selector(getHue:saturation:brightness:alpha:)])
    {
        KATFloatArray *hsba=[KATFloatArray floatArrayWithCapacity:4];
        
        CGFloat h=0,s=0,b=0,a=0;
        
        [color getHue:&h saturation:&s brightness:&b alpha:&a];
        
        [hsba put:h];
        [hsba put:s];
        [hsba put:b];
        [hsba put:a];
        
        return hsba;
    }
    
    return nil;
}


//通过颜色获取RGBA值
+ (KATFloatArray *)RGBAValueFromColor:(UIColor *)color
{
    if(color && [color respondsToSelector:@selector(getRed:green:blue:alpha:)])
    {
        KATFloatArray *rgba=[KATFloatArray floatArrayWithCapacity:4];

        CGFloat r=0,g=0,b=0,a=0;

        [color getRed:&r green:&g blue:&b alpha:&a];

        [rgba put:r];
        [rgba put:g];
        [rgba put:b];
        [rgba put:a];

        return rgba;
    }

    return nil;
}


//通过RGBA字符串获取颜色
+ (UIColor *)colorWithRGBA:(NSString *)RGBA
{
    if(RGBA && RGBA.length==8)//8位数
    {
        const char *rgba=[RGBA UTF8String];
        
        if(strlen(rgba)==8)
        {
            int r=0;//红
            int g=0;//绿
            int b=0;//蓝
            int a=0;//透明
            
            for(int i=0;i<8 ;i++)
            {
                char ch=rgba[i];
                
                int value=-1;//数值
                
                if(ch>='0' && ch<='9')//数字
                {
                    value=ch-'0';
                }
                else if(ch>='A' && ch<='F')//大写字母
                {
                    value=ch-'A'+10;
                }
                else if(ch>='a' && ch<='z')//小写字母
                {
                    value=ch-'a'+10;
                }
                else//其他内容
                {
                    return nil;
                }
                
                if(i%2==0)//十位数
                {
                    value*=16;
                }
                
                if(i/2==0)//R
                {
                    r+=value;
                }
                else if(i/2==1)//G
                {
                    g+=value;
                }
                else if(i/2==2)//B
                {
                    b+=value;
                }
                else if(i/2==3)//A
                {
                    a+=value;
                }
            }
            
            return [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a/255.0];
        }
    }
    
    return nil;
}


//通过RGB字符串获取颜色
+ (UIColor *)colorWithRGB:(NSString *)RGB
{
    return [KATColor colorWithRGBA:[NSString stringWithFormat:@"%@FF",RGB]];
}


//获取颜色的RGB字符串
+ (NSString *)RGBWithColor:(UIColor *)color
{
    KATFloatArray *rgba=[self RGBAValueFromColor:color];
    
    return [NSString stringWithFormat:@"%02X%02X%02X",(int)([rgba get:0]*255),(int)([rgba get:1]*255),(int)([rgba get:2]*255)];
}


//获取颜色的RGBA字符串
+ (NSString *)RGBAWithColor:(UIColor *)color
{
    KATFloatArray *rgba=[self RGBAValueFromColor:color];
    
    return [NSString stringWithFormat:@"%02X%02X%02X%02X",(int)([rgba get:0]*255),(int)([rgba get:1]*255),(int)([rgba get:2]*255),(int)([rgba get:3]*255)];
}




//改变颜色的透明度
+ (UIColor *)colorFromOldColor:(UIColor *)old withAlpha:(float)alpha
{
    KATFloatArray *rgba=[self RGBAValueFromColor:old];
    
    return [UIColor colorWithRed:[rgba get:0] green:[rgba get:1] blue:[rgba get:2] alpha:alpha];
}


//获取颜色的透明度
+ (CGFloat)alphaWithColor:(UIColor *)color
{
    return [[self RGBAValueFromColor:color] get:3];
}


@end
